//
//  ControlCategory.m
//  PriceOnline
//
//  Created by Le Anh Tai on 8/13/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "ControlCategory.h"

@implementation UIImage(Extension)

+ (UIImage *)imageNamed:(NSString *)name color:(UIColor *)color
{
    UIImage *image = [UIImage imageNamed:name];
    
    image = [image imageTintedWithColor:color];
    
    return image;
}

- (UIImage *)imageTintedWithColor:(UIColor *)color
{
	if (color)
    {
		// Construct new image the same size as this one.
		UIImage *image;
		UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
		CGRect rect = CGRectZero;
		rect.size = [self size];
		
		// tint the image
		[self drawInRect:rect];
		[color set];
		UIRectFillUsingBlendMode(rect, kCGBlendModeDarken);
		
		// restore alpha channel
		[self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
		
		image = UIGraphicsGetImageFromCurrentImageContext();
		UIGraphicsEndImageContext();
		
		return image;
	}
	
	return self;
}

+ (UIImage *)imageFromColor:(UIColor *)color withRect:(CGRect)imageRect
{
    UIGraphicsBeginImageContext(imageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, imageRect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

+ (UIImage *)imageFromColor:(UIColor *)color
{
    CGRect rect = RECT(0, 0, 1, 1);
    return [UIImage imageFromColor:color withRect:rect];
}

@end
